home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 033a / fildr201.zip / FILEDOOR.C next >
C/C++ Source or Header  |  1991-04-03  |  38KB  |  1,204 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dos.h>
  5. #include <time.h>
  6.  
  7. #define PAGE 22
  8. #define BIGSTR 192
  9. #define XFACTOR 150
  10. #define YFACTOR 210
  11. #define ZFACTOR 230
  12. #define ECHO_ENTER 1
  13. #define NO_ECHO_ENTER 0
  14. #define VALID_ALPHA " ~!@#$%^&()_+-=[]{};1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  15.  
  16. /*
  17.                   File Areas:
  18.  
  19.      FILEDOOR - loaded from a certain current directory...
  20.      filedoor.exe - can be in the path
  21.      filedoor.cfg - points to file area dir and menu/config directory
  22.                   - must be whereever loaded from.  Sample:
  23.                        f:\bbsfiles\   - file areas.
  24.                        d:\            - menu and xfer config files
  25.                                 filedoor.mm    main menu
  26.                                 filedoor.ma1   pre area menu
  27.                                 filedoor.ma2   post area menu/prompt
  28.                                 filedoor.mp1   pre protocol menu
  29.                                 filedoor.mp2   post protocol menu/prompt
  30.  
  31.      f:\bbsfiles   :
  32.             (areaname).D  description
  33.             1.D   FILE       (if priv is OK, get area description here.)
  34.             (areaname).(privilege required.)
  35.             1.9   DIRECTORY  (priv 9 required to use this area)
  36.                   + file1.zip
  37.                   + file2.zip
  38.                   + DESCRIBE
  39.                                 + file1.zip  (description)
  40.                                 + file2.zip  (description)
  41.  
  42.         file xfer:
  43.            user selects one character name.  This corresponds to
  44.            external BAT file or program.
  45.  
  46.            for z or y, fname.ext (on send) is file with list of files.
  47.  
  48.            Download Example of download on COM1
  49.               Z S FNAME.EXT 1
  50.            Upload Example:
  51.               Z R FNAME.EXT 1
  52.  
  53.            BAT files should be in same directory with menu files.
  54.  
  55. sending multiple files:
  56.      dsz sz @C:files.lst
  57. */
  58.  
  59. union REGS regs;
  60. #define FOSSIL int86(0x14, ®s, ®s);
  61.  
  62. int fossil_port;   /* 0 is com1, 1 is com2 */
  63. int baud_rate;
  64. long time_limit;
  65. static int more;
  66. static int local_mode;
  67. static char filepath[BIGSTR];  /* path for file area */
  68. static char menupath[BIGSTR];  /* path for menu files and temp files */
  69. static char newpath[BIGSTR];   /* path for new files (uploads) */
  70. static char protocols[BIGSTR];   /* list of valid protocols */
  71. static char current_area[BIGSTR];
  72. static char current_area_priv;
  73. static char current_area_desc[BIGSTR];
  74. unsigned todays_date;
  75.  
  76. long set_timer(long secs);
  77.  
  78. void dtr_low()
  79. {
  80.    if (local_mode) return;
  81.    regs.h.ah = 0x06;
  82.    regs.h.al = 0;
  83.    regs.x.dx = fossil_port;
  84.    FOSSIL;
  85. }
  86.  
  87. void die(rc, msg)
  88. int rc;
  89. char *msg;
  90. {
  91.    fosputs(msg);
  92.    if (rc == 9) {
  93.         fosputs("Dropping Carrier...\n");
  94.         dtr_low();
  95.    }
  96.    exit(rc);
  97. }
  98.  
  99. int dcd_status()
  100. {
  101.    if (local_mode) return(0x80);
  102.    regs.h.ah = 0x03;
  103.    regs.x.dx = fossil_port;
  104.    FOSSIL;
  105.    return (regs.h.al & 0x80);
  106. }
  107.  
  108. int remove_fossil()
  109. {
  110.    if (local_mode) return(0x80);
  111.    regs.h.ah = 0x05;
  112.    regs.x.dx = fossil_port;
  113.    FOSSIL;
  114. }
  115. int install_fossil()
  116. {
  117.    if (local_mode) return(0x80);
  118.    regs.h.ah = 0x04;
  119.    regs.x.dx = fossil_port ;
  120.    regs.x.bx = 0;
  121.    FOSSIL;
  122. }
  123.  
  124. /* rec_fossil returns -1 or character on port */
  125. int rec_fossil()
  126. {
  127.  
  128.    if (local_mode) return(-1);
  129.    regs.h.ah = 0x0c;
  130.    regs.x.dx = fossil_port;
  131.    FOSSIL;
  132.    if (regs.x.ax == -1) return(-1);
  133.    regs.h.ah = 0x02;
  134.    regs.x.dx = fossil_port;
  135.    FOSSIL;
  136.    return(regs.h.al);
  137. }
  138.  
  139. int put_fossil(comchar)
  140. int comchar;
  141. {
  142.    if (local_mode) return(comchar);
  143.    regs.h.ah = 0x01;
  144.    regs.h.al = comchar;
  145.    regs.x.dx = fossil_port;
  146.    FOSSIL;
  147.    return(comchar);
  148. }
  149.  
  150. int flush_fossil()
  151. {
  152.    if (local_mode) return(0);
  153.    regs.h.ah = 0x0A;
  154.    regs.x.dx = fossil_port;
  155.    FOSSIL;
  156. }
  157.  
  158. long
  159. set_timer(long secs)
  160. {
  161.    long ltime;
  162.    time(<ime);
  163.    ltime = ltime + secs;
  164.    return(ltime);
  165. }
  166. void
  167. sleep(long secs)
  168. {
  169.    long ltime;
  170.    ltime = set_timer(secs);
  171.    while (set_timer(0) < ltime) ;
  172.  
  173. }
  174.  
  175. char *fosgets(int len, char *allowed, int enterflg)
  176. {
  177.         static char command[BIGSTR];
  178.         char *cmdstr;
  179.         int inkey;
  180.         long timer;
  181.         int i = 0;
  182.         if (len == 0) len = BIGSTR-1;
  183.         flush_fossil();
  184.         timer = set_timer(90);
  185.         do  {
  186.                 if (!dcd_status()) {
  187.                         die(2,"\n\nExiting File System... Carrier lost.\n\n");
  188.                 }
  189.                 if (set_timer(0) > timer) {
  190.                         die(2,"\n\nExiting File System... Idle time out.\n\n");
  191.                 }
  192.                 if (set_timer(0) > time_limit) {
  193.                         die(9,"\n\nExiting File System... Time on BBS expired.\n\n");
  194.                 }
  195.                 if (kbhit())
  196.                         inkey = getch();
  197.                 else
  198.                         inkey = rec_fossil();
  199.  
  200.                 if (inkey != -1) {
  201.  
  202.                    timer = set_timer(90);
  203.                    inkey = inkey & 0x7f;
  204.  
  205.                    switch (inkey) {
  206.                        case 8 :            /* backspace */
  207.                            i-=1;
  208.                            if (i >= 0) {
  209.                                    putchar(put_fossil(8));
  210.                                    putchar(put_fossil(' '));
  211.                                    putchar(put_fossil(8));
  212.                            } else i = 0;
  213.                            break;
  214.                        case 3    :         /* control C */
  215.                            i = 0;
  216.                            fosputs(" ");
  217.                            break;
  218.                        case 0x0A :         /* cr/lf     */
  219.                        case 0x0D :
  220.                            if (enterflg == ECHO_ENTER) fosputs("\n");
  221.                        case -1   :         /* timeout */
  222.                            break;
  223.                        default :
  224.                            if (allowed[0]) {
  225.                               if (strrchr(allowed, inkey))
  226.                                  putchar(put_fossil(command[i++] = inkey));
  227.                               else
  228.                                  put_fossil(7);
  229.                            } else
  230.                               putchar(put_fossil(command[i++] = inkey));
  231.                    }
  232.                 }
  233.  
  234.                 if (i >= BIGSTR-1) {
  235.                         fosputs("\n  Too many characters entered...\n");
  236.                         inkey = 3;
  237.                 }
  238.  
  239.         } while ((inkey != 0x0D) && (inkey != 0x0A)
  240.             && (inkey != 3) && (i<len));
  241.                                              /* enter or ctrl c */
  242.         if (inkey == 3) {
  243.            command[0] = 3;
  244.            i = 1;
  245.         } else
  246.            if (((inkey == 0x0D) || (inkey == 0x0A)) &&
  247.               (enterflg == NO_ECHO_ENTER) && (i==0)) command[i++] = '\n';
  248.  
  249.         command[i] = '\0';
  250.  
  251.         cmdstr = command;
  252.         while ((*cmdstr == ' ') && (strlen(cmdstr) > 1)) cmdstr++;
  253.         return(cmdstr);
  254. }
  255.  
  256. char
  257. more_prompt(prompt)
  258. char *prompt;
  259. {
  260.         char ch;
  261.         int i, j;
  262.         more = 0;
  263.         fosputs(prompt);
  264.         ch = fosgets(1,"YyNn ",NO_ECHO_ENTER)[0];
  265.         if (ch == '\n') j = strlen(prompt); else j = strlen(prompt) + 1;
  266.         if (ch == 3) ch = 'N';
  267.         if (ch) for (i=0 ; i<j; i++) {
  268.             putchar(put_fossil(8));
  269.             putchar(put_fossil(' '));
  270.             putchar(put_fossil(8));
  271.         }
  272.         if (ch == ' ')
  273.             more = PAGE-1;
  274.         else
  275.             more = 0;
  276.         return(toupper(ch));
  277. }
  278.  
  279. int fosputs(text)
  280. char *text;
  281. {
  282.         while (*text) {
  283.                 printf("%c",*text);
  284.                 if (rec_fossil() == 3)
  285.                    return(0);
  286.                 if (*text == '\n') {
  287.                    put_fossil(0x0D);
  288.                    put_fossil(*text);
  289.                    more++;
  290.                    if (more == PAGE) {
  291.                       more = 0;
  292.                       if (more_prompt("[More? y, n, <space>] : ") == 'N')
  293.                          return(0);
  294.                    }
  295.                 } else put_fossil(*text);
  296.                 text++;
  297.         }
  298.         return(1);
  299. }
  300.  
  301. int fixup(str)
  302. char *str;
  303. {
  304.         int i;
  305.         for (i = strlen(str)-1 ; i>=0; i--)
  306.                 if ((str[i] == 0x0D) || (str[i] == 0x0A) || (str[i] == ' '))
  307.                         str[i] = '\0';
  308. }
  309.  
  310. long
  311. exist(filename)
  312. char *filename;
  313. {
  314.         struct find_t find, *finda;
  315.  
  316.         finda = &find;
  317.         if( !_dos_findfirst( filename, _A_NORMAL, &find ) ){
  318.                 return(finda->size);
  319.         }else
  320.                 return(0);
  321.  
  322. }
  323.  
  324. char *drivename(wholename)
  325. char *wholename;
  326. {
  327.    static char path[80];
  328.    int i, last=0;
  329.    for (i=0;(i<strlen(wholename)) && (wholename[i] != ':'); i++) ;
  330.    path[0] = '\0';
  331.    if (i<strlen(wholename)) strncpy(path,wholename,i+1);
  332.    path[i+1] = '\0';
  333.    if (path[0] == ':') path[0] = '\0';
  334.    return(path);
  335. }
  336. char *pathname(wholename)
  337. char *wholename;
  338. {
  339.    static char path[80];
  340.    int i, last=0;
  341.    for (i=0; i<strlen(wholename); i++)
  342.       if (wholename[i] == '\\')
  343.          last = i;
  344.    path[0] = '\0';
  345.  
  346.    if ((wholename[last] =='\\') && ((wholename[last-1]==':') || last==0))
  347.       last++;
  348.    if (wholename[last]==':')
  349.       last = 0;
  350.  
  351.    strncpy(path, wholename, last);
  352.    path[last] = '\0';
  353.    return(path);
  354. }
  355.  
  356. int getconfig()
  357. {
  358.         FILE *cfg;
  359.         char line[BIGSTR];
  360.         char *fname;
  361.         int fdrive;
  362.         if (!(fname = getenv("FILEDOOR"))) return(0);
  363.         if (cfg = fopen(fname,"r")) {
  364.                 strcpy (filepath, fgets(line, BIGSTR-1, cfg));
  365.                 strcpy (menupath, fgets(line, BIGSTR-1, cfg));
  366.                 strcpy (newpath,  fgets(line, BIGSTR-1, cfg));
  367.                 strcpy (protocols,  fgets(line, BIGSTR-1, cfg));
  368.                 fixup (filepath);
  369.                 fixup (menupath);
  370.                 fixup (newpath);
  371.                 fixup (protocols);
  372.                 fclose(cfg);
  373.                 if (fdrive = drivename(filepath)[0])
  374.                    _chdrive( fdrive = (toupper(fdrive) - 'A')+1 );
  375.                 chdir(pathname(filepath));
  376.                 return(1);
  377.         } else return(0);
  378. }
  379.  
  380. int showmenu(str)
  381. char *str;
  382. {
  383.         FILE *menu;
  384.         char line[BIGSTR], fname[BIGSTR], *findit;
  385.         int i;
  386.         more = 0;
  387.         sprintf(fname, "%s%s%s", menupath, "FILEDOOR.", str);
  388.         if (menu = fopen(fname,"r")) {
  389.                 while (fgets(line, BIGSTR-1, menu)) {
  390.                     for (i=0; i<strlen(line); i++)
  391.                             if (findit = strrchr(line, '~')) *findit = '\0';
  392.                     if (!fosputs(line)) break;
  393.                 }
  394.                 fclose(menu);
  395.         }
  396. }
  397.  
  398. char
  399. *bbs_area_dir(search)
  400. char *search;
  401. {
  402.         static char apath[BIGSTR];
  403.         if (search[0])
  404.            sprintf(apath, "%s.%c\\%s", current_area, current_area_priv, search);
  405.         else
  406.            sprintf(apath, "%s.%c\\*.*", current_area, current_area_priv);
  407.         return(apath);
  408. }
  409.  
  410. char
  411. *bbs_file(filename)
  412. char *filename;
  413. {
  414.         static char apath[BIGSTR];
  415.         sprintf(apath, "%s.%c\\%s", current_area, current_area_priv,
  416.                    filename);
  417.         return(apath);
  418. }
  419.  
  420. char
  421. *bbs_file_desc(filename)
  422. char *filename;
  423. {
  424.         static char apath[BIGSTR];
  425.         sprintf(apath, "%s.%c\\DESCRIBE\\%s", current_area,
  426.                    current_area_priv, filename);
  427.         return(apath);
  428. }
  429. void
  430. print_time(char *fname, long fsize, char opt)
  431. {
  432.         static char workstr[BIGSTR];
  433.         long worktime, worktime2, factor;
  434.         switch (opt) {
  435.                 case 'X' :
  436.                 factor = XFACTOR;
  437.                 break;
  438.                 case 'Y' :
  439.                 factor = YFACTOR;
  440.                 break;
  441.                 case 'Z' :
  442.                 factor = ZFACTOR;
  443.                 break;
  444.                 default  :
  445.                 factor = 999999;
  446.                 ;
  447.         }
  448.         worktime = ((fsize*240) / ((baud_rate/10)*factor)) / 60;
  449.         sprintf(workstr, "File %s in area %s : %ld bytes, %ld minute(s)\n", strupr(fname), current_area, fsize, worktime+1);
  450.         fosputs(workstr);
  451.         if (opt == 'X') {
  452.            factor = ZFACTOR;
  453.            worktime2 = worktime - (((fsize*240) / ((baud_rate/10)*factor)) / 60);
  454.            if (worktime2 > 1) {
  455.               sprintf(workstr, "Using ZMODEM would SAVE %ld minute(s)\n", worktime2);
  456.               fosputs(workstr);
  457.            }
  458.            factor = YFACTOR;
  459.            worktime2 = worktime - (((fsize*240) / ((baud_rate/10)*factor)) / 60);
  460.            if (worktime2 > 1) {
  461.               sprintf(workstr, "  And YMODEM would SAVE %ld minute(s)\n", worktime2);
  462.               fosputs(workstr);
  463.            }
  464.         }
  465. }
  466.  
  467. long
  468. find_file(char *fname, int priv)
  469. {
  470.        struct find_t afind, *afinda;
  471.        long fsize;
  472.        char *p, savearea[BIGSTR];
  473.        strcpy(savearea, current_area);
  474.        afinda = &afind;
  475.        /* FIRST CHECK THE CURRENT AREA!!! - allows duplicate file names */
  476.        if (fsize = exist(bbs_file(fname))) return(fsize);
  477.        if( !_dos_findfirst( "*.*", _A_SUBDIR, &afind ) )
  478.        {
  479.            if ((afinda->attrib & _A_SUBDIR)  && (afinda->name[0] != '.')) {
  480.               p = strpbrk(afinda->name, ".");
  481.               p[0] = '\0';
  482.               /* only look in an current area once */
  483.               if ( (stricmp(savearea, afinda->name)) && (areaok(afinda->name, priv))) {
  484.                  strcpy(current_area, afinda->name);
  485.                  if (fsize = exist(bbs_file(fname))) return(fsize);
  486.               }
  487.            }
  488.            while( !_dos_findnext( &afind ) )
  489.               if ((afinda->attrib & _A_SUBDIR) && (afinda->name[0] != '.')){
  490.                  p = strpbrk(afinda->name, ".");
  491.                  p[0] = '\0';
  492.                  /* only look in an current area once */
  493.                  if ( (stricmp(savearea, afinda->name)) && (areaok(afinda->name, priv))) {
  494.                     strcpy(current_area, afinda->name);
  495.                     if (fsize = exist(bbs_file(fname))) return(fsize);
  496.                  }
  497.               }
  498.        }
  499.        return(0);
  500. }
  501.  
  502. int
  503. add_file_to_list(int append_flag, char *filename, char popt, int priv)
  504. {
  505.         char listfname[BIGSTR];
  506.         char workarea[BIGSTR];
  507.         FILE *listf;
  508.         int apriv;
  509.         long fsize;
  510.  
  511.         sprintf(listfname, "%s%s", menupath, "FILEDOOR.FLS");
  512.         if (append_flag)
  513.                 listf = fopen(listfname, "a");
  514.         else
  515.                 listf = fopen(listfname, "w");
  516.  
  517.     /* SALZO1 CODE HERE TO SCAN ALL DIRECTORIES */
  518.         strcpy(workarea, current_area);
  519.         apriv = current_area_priv;
  520.         if (fsize = find_file(filename, priv)) {
  521.                 fprintf(listf, "%s\n", bbs_file(filename));
  522.                 fclose(listf);
  523.                 print_time(filename, fsize, popt);
  524.                 current_area_priv = apriv;
  525.                 strcpy(current_area, workarea);
  526.                 return(1);
  527.         }
  528.         sprintf(listfname, "** FILE \"%s\" NOT FOUND **\n", strupr(filename));
  529.         fosputs(listfname);
  530.         fclose(listf);
  531.         current_area_priv = apriv;
  532.         strcpy(current_area, workarea);
  533.         return(0);
  534. }
  535.  
  536. /*
  537.            Download Example of download on COM1
  538.               d:\Z S FNAME.EXT 1
  539. */
  540. void
  541. download(protocol, filename)
  542. char protocol;
  543. char *filename;
  544. {
  545.         char commandline[BIGSTR];
  546.         sprintf(commandline, "%s%c S %s %d",
  547.                 menupath, protocol, filename, fossil_port+1);
  548.         fosputs("\nPrepare system for DOWNLOAD now.\n\n");
  549.         sleep(2);       /* try to fix screwed up text problem */
  550.         remove_fossil();
  551.         if (!local_mode) system(commandline);
  552.         install_fossil();
  553.         sleep(2);       /* try to fix screwed up text problem */
  554.         while (rec_fossil() != -1) ; /* flush uart */
  555.         put_fossil(7);
  556.         sleep(1);       /* try to fix screwed up text problem */
  557.         put_fossil(7);
  558. }
  559.  
  560. void
  561. upload(protocol, filename)
  562. char protocol;
  563. char *filename;
  564. {
  565.         char commandline[BIGSTR];
  566.         FILE *describe;
  567.         long time1, time2;
  568.         sprintf(commandline, "%s%s", newpath, filename);
  569.         if (!exist(commandline)) {
  570.            sprintf(commandline, "%s%s\\%s", newpath, "DESCRIBE", filename);
  571.            if (describe = fopen(commandline, "w")) {
  572.                fosputs("Enter description : ");
  573.                strcpy(commandline, fosgets(50, VALID_ALPHA, ECHO_ENTER));
  574.                if (commandline[0] == 3) {
  575.                   fclose(describe);
  576.                   return;
  577.                }
  578.                fprintf(describe, "%s", commandline);
  579.                fclose(describe);
  580.            }
  581.            sprintf(commandline, "%s%c R %s%s %d",
  582.                menupath, protocol, newpath, filename, fossil_port+1);
  583.            fosputs("\nInitiate UPLOAD now.\n\n");
  584.            sleep(2);       /* try to fix screwed up text problem */
  585.            remove_fossil();
  586.            time1 = set_timer(0);
  587.            if (!local_mode) system(commandline);
  588.            install_fossil();
  589.            time2 = set_timer(0);
  590.            time_limit = time_limit + (time2 - time1);
  591.            put_fossil(7);
  592.            put_fossil(7);
  593.            put_fossil(7);
  594.         } else {
  595.            fosputs("\n** CANNOT CREATE FILE - MAY ALREADY EXIST **\n");
  596.         }
  597. }
  598.  
  599. char
  600. *filedesc(filename)
  601. char *filename;
  602. {
  603.         static char desc[BIGSTR];
  604.         FILE *areaf;
  605.         int i;
  606.         desc[0] = '\0';
  607.         if (areaf = fopen(bbs_file_desc(filename), "r")) {
  608.                 fgets(desc, 50, areaf);
  609.                 for (i = strlen(desc)-1 ; i>=0; i--)
  610.                     if ((desc[i] == 0x0D) || (desc[i] == 0x0A))
  611.                          desc[i] = '\0';
  612.                 fclose(areaf);
  613.         }
  614.         return(desc);
  615. }
  616.  
  617. /* Takes unsigned date in the format:               fedcba9876543210
  618.  * d=1-31, m=1-12, y=0-119 (1980-2099)              yyyyyyymmmmddddd
  619.  * Changes to a 9-byte string:                      mm/dd/yy
  620.  */
  621. char *datestr( unsigned d, char *buf )
  622. {
  623.     sprintf( buf, "%2d/%02.2d/%02.2d",
  624.              (d >> 5) & 0x0f, d & 0x1f, (d >> 9) + 80 );
  625.     return buf;
  626. }
  627.  
  628. unsigned int_date(unsigned d)
  629. {
  630.     unsigned temp;
  631.     temp =  ((d >> 9) * 360) + (((d >> 5) & 0x0f) * 30) + min ((d & 0x1f), 30);
  632. /*    printf("date: %d\n", temp);*/
  633.     return(temp);
  634. }
  635.  
  636.  
  637. int
  638. print_file_info(filename)
  639. char *filename;
  640. {
  641.         struct find_t find, *finda;
  642.         long size;
  643.         unsigned date;
  644.         char fname[BIGSTR], dates[10];
  645.         finda = &find;
  646.         if( !_dos_findfirst( bbs_file(filename), _A_NORMAL, &find ) ) {
  647.                 size = (finda->size / 1025) + 1 ;
  648.                 date = finda->wr_date;
  649.         }else{
  650.                 size = 0;
  651.                 date = 0;
  652.         }
  653.         sprintf(fname, " %-12s  %6s  %3ldk  %s\n", filename, datestr(date, dates),
  654.         size, filedesc(filename));
  655.         return(fosputs(fname));
  656. }
  657.  
  658. void
  659. make_ref()
  660. /* KLUGE to get today's file write date */
  661. {
  662.         FILE *ref;
  663.         struct find_t find, *finda;
  664.         char temp[BIGSTR];
  665.         todays_date = 0xFFFF;
  666.         sprintf(temp, "%s%s", menupath, "FILEDOOR.REF");
  667.         ref = fopen(temp, "w");
  668.         fclose(ref);
  669.         finda = &find;
  670.         if( !_dos_findfirst( temp, _A_NORMAL, &find ) )
  671.             todays_date = int_date(finda->wr_date) ; /* 1/2/91 */
  672.         /*    todays_date = finda->wr_date; */
  673. }
  674.  
  675. int areaok(tmp, priv)
  676. char *tmp;
  677. int priv;
  678. {
  679.     struct find_t find, *finda;
  680.     char timp;
  681.     char work[BIGSTR];
  682.  
  683.     finda = &find;
  684.     strcpy(work, tmp);
  685.     strcat(work, ".*");
  686.     if ( !_dos_findfirst( work, _A_SUBDIR, &find ) )
  687.         if ((finda->attrib & _A_SUBDIR) &&
  688.            (((timp = strpbrk(finda->name, ".")[1])-'0') <= priv)){
  689.            current_area_priv = timp;
  690.            return(1);
  691.            }
  692.         else
  693.            if ( !_dos_findnext( &find ) )
  694.               if ((finda->attrib & _A_SUBDIR) &&
  695.                  (((timp = strpbrk(finda->name, ".")[1])-'0') <= priv)){
  696.                  current_area_priv = timp;
  697.                  return(1);
  698.                  }
  699.     return(0);
  700. }
  701.  
  702.  
  703. int
  704. list_area_engine(unsigned age, char *search, char *init, int *found)
  705. {
  706.     struct find_t find, *finda;
  707.     finda = &find;
  708.        if (!age) age = 0xFFFF;
  709.        if( !_dos_findfirst( bbs_area_dir(search), _A_NORMAL, &find ) )
  710.        {
  711.            fosputs(init);
  712.  
  713.            if ((todays_date - int_date(finda->wr_date) <= age)) {
  714.               *found = 1;
  715.               if (!print_file_info(finda->name)) return(0);
  716.            }
  717.            while( !_dos_findnext( &find ) ) {
  718.               if ((todays_date - int_date(finda->wr_date) <= age)) {
  719.                  *found = 1;
  720.                  if (!print_file_info(finda->name)) return(0);
  721.               }
  722.            }
  723.        }
  724.        return(1);
  725. }
  726.  
  727.  
  728. void
  729. list_area(unsigned age, char *search, char *init, int priv)
  730. {
  731.     struct find_t afind, *afinda;
  732.     int found;
  733.     int apriv;
  734.     char *p;
  735.     char work_area[BIGSTR], workbuff[BIGSTR];
  736.     afinda = &afind;
  737.  
  738.     found = 0;
  739.     strcpy(work_area, current_area);
  740.     apriv = current_area_priv;
  741.     if (!((age) || (search[0]))) {  /*do the usual stuff*/
  742.  
  743.        if (!list_area_engine(age, search, init, &found)) goto cleanup;
  744.  
  745.     } else { /* NEW SEARCH SALZO search each area for the files */
  746.        if( !_dos_findfirst( "*.*", _A_SUBDIR, &afind ) )
  747.        {
  748.            if ((afinda->attrib & _A_SUBDIR)  && (afinda->name[0] != '.')) {
  749.               p = strpbrk(afinda->name, ".");
  750.               p[0] = '\0';
  751.               if (areaok(afinda->name, priv)) {
  752.                  strcpy(current_area, afinda->name);
  753.                  sprintf(workbuff, "** Looking in area %s...\n", current_area);
  754.                  if (!fosputs(workbuff)) goto cleanup;
  755.                  if (!list_area_engine(age, search, init, &found)) goto cleanup;
  756.               }
  757.            }
  758.            while( !_dos_findnext( &afind ) )
  759.               if ((afinda->attrib & _A_SUBDIR) && (afinda->name[0] != '.')){
  760.                  p = strpbrk(afinda->name, ".");
  761.                  p[0] = '\0';
  762.                  if (areaok(afinda->name, priv)) {
  763.                     strcpy(current_area, afinda->name);
  764.                     sprintf(workbuff, "** Looking in area %s...\n", current_area);
  765.                     if (!fosputs(workbuff)) goto cleanup;
  766.                     if (!list_area_engine(age, search, init, &found)) goto cleanup;
  767.                  }
  768.               }
  769.        }
  770.     }   /* end of new code SALZO */
  771.  
  772.     if (!found)
  773.        fosputs("\n** No files found **\n");
  774.     else
  775.        if (more > 0) more_prompt("\nPress Enter to continue...");
  776. cleanup:
  777.     strcpy(current_area, work_area);
  778.     current_area_priv = apriv;
  779. }
  780.  
  781. char
  782. *areadesc(area)
  783. char *area;
  784. {
  785.         static char desc[BIGSTR];
  786.         char fname[BIGSTR];
  787.         FILE *areaf;
  788.         int i;
  789.         desc[0] = '\0';
  790.         sprintf(fname, "%s.%s", area, "D");
  791.         if (areaf = fopen(fname, "r")) {
  792.                 fgets(desc, 60, areaf);
  793.                 for (i = strlen(desc)-1 ; i>=0; i--)
  794.                     if ((desc[i] == 0x0D) || (desc[i] == 0x0A))
  795.                          desc[i] = '\0';
  796.                 fclose(areaf);
  797.         }
  798.         return(desc);
  799. }
  800.  
  801. int
  802. print_area_info(area, priv)
  803. char *area;
  804. int priv;
  805. {
  806.         int apriv;
  807.         char fname[BIGSTR], *p;
  808.  
  809.         p = strpbrk(area, ".");
  810.         apriv = p[1] - '0';
  811.         p[0] = '\0';
  812.  
  813.         if (apriv > priv) return(0);
  814.  
  815.         sprintf(fname, "  %-8s  -  %s\n", area, areadesc(area));
  816.         return(fosputs(fname));
  817. }
  818.  
  819. showareas(priv)
  820. int priv;
  821. {
  822.  
  823.   /*
  824.   get first dir, extract name, check priv, show if ok, show name.d
  825.   contents as description of the area - print only enough bytes to fit on
  826.   line.
  827.   do the same for each successive directory.
  828.   */
  829.     struct find_t find, *finda;
  830.     char dirname[BIGSTR];
  831.     FILE *AREA;
  832.  
  833.     finda = &find;
  834.     if( !_dos_findfirst( "*.*", _A_SUBDIR, &find ) )
  835.     {
  836.         if ((finda->attrib & _A_SUBDIR)  && (finda->name[0] != '.'))
  837.            print_area_info(finda->name, priv) ;
  838.         while( !_dos_findnext( &find ) )
  839.            if ((finda->attrib & _A_SUBDIR) && (finda->name[0] != '.'))
  840.               print_area_info(finda->name, priv) ;
  841.     }
  842. }
  843.  
  844. int
  845. select_area(priv)
  846. int priv;
  847. {
  848.    int tries;
  849.    char workbuff[BIGSTR];
  850.    tries = 0;
  851.    do {
  852.       showmenu("MA1");
  853.       showareas(priv);
  854.       showmenu("MA2");
  855.       strcpy (workbuff, fosgets(0,
  856.       "~!@#$%^&()_+-=[]{};1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
  857.          ECHO_ENTER));
  858.       if ((workbuff[0] == 0) || (workbuff[0] == 3))
  859.          return(0);
  860.       if (((workbuff[0] == 'q') || (workbuff[0] == 'Q')) &&
  861.                (!workbuff[1]))
  862.          return('Q');
  863.       if (!areaok(workbuff, priv)) {
  864.          fosputs("\n** INVALID AREA ENTERED **\n");
  865.          tries++;
  866.          if (tries > 4) die(5, "\n\nExiting File System... Failure to select area.\n\n");
  867.       }
  868.    } while (!areaok(workbuff, priv));
  869.  
  870.    strcpy (current_area, workbuff);
  871.    strcpy(current_area_desc, areadesc(strupr(current_area)));
  872.    return(1);
  873. }
  874.  
  875. void
  876. ansi_cls(char *term)
  877. {
  878.    char workbuff[BIGSTR];
  879.    if (toupper(term[0]) == 'V') {  /* if vt100, clear the screen*/
  880.       sprintf(workbuff, "%c[2J", 0x1B);
  881.       fosputs(workbuff);
  882.    }
  883. }
  884.  
  885. main(argc, argv, envp)
  886.    int argc;
  887.    char *argv[];
  888.    char **envp;
  889. {
  890.    static char userid[80], term[80], init_area[80];
  891.    char filechars[BIGSTR];
  892.    char workbuff[BIGSTR];
  893.    char workarea[BIGSTR];
  894.    unsigned opt;
  895.    int filecount;
  896.    long fsize, tmpl;
  897.    int apriv;
  898.    int priv=0;
  899.    fossil_port = 0;
  900.    baud_rate = 2400;
  901.  
  902.    if ((argc>=2) && (**++argv=='?')) {
  903.       printf("FILEDOOR, usage: filedoor -p port -u userid -t termtype -l priv -Z\n");
  904.       printf("          ports are 1, 2, 3, 4   \n");
  905.       exit(1);
  906.    } else {
  907.  
  908.       /* Get all the dash (-) options.  */
  909.       while (**argv=='-') {
  910.          switch (*++*argv) {
  911.          case 'l':
  912.          case 'L':
  913.             if (!(*++*argv)) {
  914.                argv++;
  915.                argc--;
  916.                if (argc <= 1) {
  917.                   printf("No level entered.  Enter FILEDOOR ? for help.\n");
  918.                   exit(1);
  919.                }
  920.             }
  921.             priv = atoi(*argv);
  922.             printf("Level         : %d\n",priv);
  923.             break;
  924.          case 'u':
  925.          case 'U':
  926.             if (!(*++*argv)) {
  927.                argv++;
  928.                argc--;
  929.                if (argc <= 1) {
  930.                   printf("No userid entered.  Enter FILEDOOR ? for help.\n");
  931.                   exit(1);
  932.                }
  933.             }
  934.             strcpy(userid, *argv);
  935.             printf("USERID        : %s\n",userid);
  936.             break;
  937.          case 'a':
  938.          case 'A':
  939.             if (!(*++*argv)) {
  940.                argv++;
  941.                argc--;
  942.                if (argc <= 1) {
  943.                   printf("No init area entered.  Enter FILEDOOR ? for help.\n");
  944.                   exit(1);
  945.                }
  946.             }
  947.             strcpy(init_area, *argv);
  948.             printf("Init Area        : %s\n",init_area);
  949.             break;
  950.          case 't':
  951.          case 'T':
  952.             if (!(*++*argv)) {
  953.                argv++;
  954.                argc--;
  955.                if (argc <= 1) {
  956.                   printf("No term type entered.  Enter FILEDOOR ? for help.\n");
  957.                   exit(1);
  958.                }
  959.             }
  960.             strcpy(term, *argv);
  961.             printf("TERMINAL      : %s\n",term);
  962.             break;
  963.          case 'p':
  964.          case 'P':
  965.             if (!(*++*argv)) {
  966.                argv++;
  967.                argc--;
  968.                if (argc <= 1) {
  969.                   printf("No port entered.  Enter FILEDOOR ? for help.\n");
  970.                   exit(1);
  971.                }
  972.             }
  973.             fossil_port=atoi(*argv) - 1 ;
  974.             printf("Port          : %d\n",fossil_port);
  975.             if ((*argv[0] < '0') || (*argv[0] > '9') || (strlen(*argv) > 1)) {
  976.                printf("Invalid COM port.  0 thru 9 are supported.\n");
  977.                exit(1);
  978.             }
  979.             break;
  980.          case 'b':
  981.          case 'B':
  982.             if (!(*++*argv)) {
  983.                argv++;
  984.                argc--;
  985.                if (argc <= 1) {
  986.                   printf("No Baud Rate Entered.  Enter FILEDOOR ? for help.\n");
  987.                   exit(1);
  988.                }
  989.             }
  990.             baud_rate=atoi(*argv);
  991.             printf("Baud Rate     : %d\n",baud_rate);
  992.             break;
  993.          case 'o':
  994.          case 'O':
  995.             if (!(*++*argv)) {
  996.                argv++;
  997.                argc--;
  998.                if (argc <= 1) {
  999.                   printf("No time limit entered.  Enter FILEDOOR ? for help.\n");
  1000.                   exit(1);
  1001.                }
  1002.             }
  1003.             time_limit = (long) atoi(*argv) * 60;
  1004.             printf("Seconds       : %ld\n", time_limit);
  1005.             time_limit = set_timer(time_limit);
  1006.             printf("Time Now      : %ld\n",set_timer(0));
  1007.             printf("Time Out At   : %ld\n",time_limit);
  1008.             break;
  1009.          case 'z':
  1010.          case 'Z':
  1011.             local_mode = 1;
  1012.             break;
  1013.          default:
  1014.             ;
  1015.          } /* endswitch */
  1016.          argv++;  /* point to next arg */
  1017.          argc--;  /* argc needs to reflect positional arg count */
  1018.       }  /* end while */
  1019.    }
  1020.  
  1021.    if (getconfig()) {
  1022.    ansi_cls(term);
  1023.    showmenu("M00");
  1024.    if (userid[0]) userid[0] = toupper(userid[0]);
  1025.    fosputs(userid);fosputs("!\n");
  1026.  
  1027.    strcpy(filechars,"abcdefghijklmnopqrstuvwxyz");
  1028.    strcat(filechars,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  1029.    strcat(filechars,"1234567890-=!@#$%^&*?()_+.;\"'|][{}");
  1030.  
  1031.    make_ref();
  1032.  
  1033.    /* go immediately to select area... then into the loop. */
  1034.  
  1035. /* new stuff 1/13/91 */
  1036.    if ((init_area[0] != '\0') && areaok(init_area, priv)) {
  1037.       strcpy (current_area, init_area);
  1038.       strcpy(current_area_desc, areadesc(strupr(current_area)));
  1039.    } else {
  1040.       if (!(opt = select_area(priv)) || (opt == 'Q')) die(5, "\n\nExiting File System... No area selected.\n\n");
  1041.    }
  1042.  
  1043.  
  1044.    while (1) {
  1045.         /* reset 'more' counter */
  1046.         more = 0;
  1047.         tmpl = time_limit - set_timer(0);
  1048.         tmpl = tmpl / 60;
  1049.         sprintf(workbuff, "\nYou have %ld minutes remaining.\n", tmpl);
  1050.         fosputs(workbuff);
  1051.         sprintf(workbuff, "\nArea %s - %s\n", current_area, current_area_desc);
  1052.         fosputs(workbuff);
  1053.         showmenu("MM"); /*  show main menu */
  1054.         opt = fosgets(1,"qQaAfFlLuUdDnN?hH", ECHO_ENTER)[0];
  1055.         fosputs("\n");
  1056.         switch (opt) {
  1057.             case 'Q':
  1058.             case 'q':
  1059.                 showmenu("MQ1");
  1060.                 opt = fosgets(1, "QqAaGg", ECHO_ENTER)[0];
  1061.                 fosputs("\n");
  1062.                 more = 0;
  1063.                 switch (opt=toupper(opt)) {
  1064.                 case 'A':
  1065.                    if (select_area(priv) == 'Q') exit(0);
  1066.                    break;
  1067.                 case 'G':       /* drop dtr, hang up fone... */
  1068.                    fosputs("\n\nTHANKS for Calling!!!\n\n");
  1069.                    dtr_low();
  1070.                 case 'Q':
  1071.                    exit(0);
  1072.                 default:
  1073.                    ;
  1074.                 }
  1075.                 break;
  1076.             case 'A':
  1077.             case 'a':
  1078.                 if (select_area(priv) == 'Q') exit(0);
  1079.                 /* get area choice */
  1080.                 /* log area choice, if not valid - display message */
  1081.                 /* not valid means either does not exist or priv too low */
  1082.                 break;
  1083.             case 'F':
  1084.             case 'f':
  1085.                 more = 0;
  1086.                 /* ask for search criteria */
  1087.                 fosputs("\nEnter filename search criteria (eg, *.zip) : ");
  1088.                 /* search current area , list files with description */
  1089.                 strcpy(workbuff, fosgets(0, filechars, ECHO_ENTER));
  1090.                 if (workbuff[0] == 3) break;
  1091.                 if (!workbuff[0]) strcpy(workbuff, "*.*");
  1092.                 list_area(0, workbuff, "", priv);
  1093.                 /* use 'more' after each PAGE lines */
  1094.                 break;
  1095.             case 'L':
  1096.             case 'l':
  1097.                 more = 0;
  1098.                 list_area(0, "", "\n", priv);
  1099.                 break;
  1100.             case 'N':
  1101.             case 'n':
  1102.                 more = 0;
  1103.                 /* ask for number of days.  Compare (today-n) with date
  1104.                    of each file's **directory**.  If file's directory
  1105.                    date is greater, list that file in current area.
  1106.                    Use first found area with correct priv if no area
  1107.                    logged */
  1108.                 fosputs("\nEnter file age (in days) to include in search : ");
  1109.                 strcpy(workbuff, fosgets(0, "1234567890", ECHO_ENTER));
  1110.                 if (workbuff[0] == 3) break;
  1111.                 opt = atoi(workbuff);
  1112.                 fosputs("Enter filename search criteria (eg, *.zip) : ");
  1113.                 strcpy(workbuff, fosgets(0, filechars, ECHO_ENTER));
  1114.                 if (workbuff[0] == 3) break;
  1115.                 if (!workbuff[0]) strcpy(workbuff, "*.*");
  1116.                 list_area(opt, workbuff, "", priv);
  1117.                 break;
  1118.             case 'D':
  1119.             case 'd':
  1120.                 more = 0;
  1121.                 filecount = 0;
  1122.                 /* ask for protocol */
  1123.                 showmenu("MP1");
  1124.                 opt = fosgets(1, protocols, ECHO_ENTER)[0];
  1125.                 if ((!opt) || (opt==3)) break;
  1126.                 /* fosputs("\nEnter a file name for download : ");*/
  1127.                 /* fosputs("\nSpecify a file name from the current area and press <enter> : ");*/
  1128.                 fosputs("\nSpecify a file name from any area and press <enter> : ");
  1129.                 strcpy(workbuff, fosgets(0, filechars, ECHO_ENTER));
  1130.                 if (workbuff[0] == 3) break;
  1131.                 switch (opt=toupper(opt)) {
  1132.                     case 'X':
  1133.                 /* SALZO1 CODE HERE TO SCAN ALL DIRECTORIES */
  1134.                         strcpy(workarea, current_area);
  1135.                         apriv = current_area_priv;
  1136.                         if (fsize = find_file(workbuff, priv)) {
  1137.                            print_time(workbuff, fsize, 'X');
  1138.                            download(opt, bbs_file(workbuff));
  1139.                         } else
  1140.                            fosputs("** FILE NOT FOUND **\n");
  1141.                         current_area_priv = apriv;
  1142.                         strcpy(current_area, workarea);
  1143.                         break;
  1144.                     case 'Z':
  1145.                     case 'Y':
  1146.                         filecount += add_file_to_list(0, workbuff, (char) opt, priv);
  1147.                         do {
  1148.                            more = 0;
  1149.                            /* sprintf(workbuff, "File %d (blank begins download) : ", filecount+1);*/
  1150.                            sprintf(workbuff, "Specify file #%d (blank to begin download) and press <enter> : ", filecount+1);
  1151.                            fosputs(workbuff);
  1152.                            strcpy(workbuff, fosgets(0, filechars, ECHO_ENTER));
  1153.                            if (workbuff[0] == 3) break;
  1154.                            if (workbuff[0]) filecount += add_file_to_list(1, workbuff, (char) opt, priv);
  1155.                         } while (workbuff[0]);
  1156.                         if (workbuff[0] == 3) break;
  1157.                         sprintf(workbuff, "%s%s", menupath, "FILEDOOR.FLS");
  1158.                         if (filecount)
  1159.                            download(opt, workbuff);
  1160.                         else
  1161.                            fosputs("\n** No files to DOWNLOAD **\n");
  1162.                         break;
  1163.                     default:
  1164.                     ;
  1165.                 }
  1166.                 /* ask for file name, search for file if not found, ask
  1167.                    again until null string entered.  */
  1168.                 /* if Y, B, or Z, ask for multiple files, storing in
  1169.                         file menudir\download.lst */
  1170.                 /* if X, just ask for one file name */
  1171.                 break;
  1172.             case 'U':
  1173.             case 'u':
  1174.                 more = 0;
  1175.                 /* ask for protocol */
  1176.                 showmenu("MP1");
  1177.                 opt = fosgets(1, protocols, ECHO_ENTER)[0];
  1178.                 if ((!opt) || (opt==3)) break;
  1179.                 fosputs("\nEnter a file name for upload : ");
  1180.                 strcpy(workbuff, fosgets(0, filechars, ECHO_ENTER));
  1181.                 if (!workbuff[0] || (workbuff[0] == 3)) break;
  1182.                 upload(opt, workbuff);
  1183.                 /* upload to new file area only.  NEW is new file area */
  1184.                 /* ask for file name and description.  Create necessary */
  1185.                 /* file/directory structure */
  1186.                 break;
  1187.             case 'h':
  1188.             case 'H':
  1189.             case '?':
  1190.                 ansi_cls(term);
  1191.                 more = 0;
  1192.                 showmenu("MH1");
  1193.                 if (more > 0) more_prompt("\nPress Enter to continue...");
  1194.                 ansi_cls(term);
  1195.                 /*fosputs(userid);fosputs("!\n");*/
  1196.                 break;
  1197.             default :
  1198.                 ;
  1199.         }
  1200.    /*  if (opt == 'x') system("c:\\util\\dsz port 2 sx c:\\x10\\fossil\\filedoor.mm") ;*/
  1201.    }} else fosputs("\n\nfiledoor:  CANT FIND CONFIGURATION FILE!!!\n\n");
  1202.    exit(3);
  1203. }
  1204.